const chalk = require('chalk')
const _ = require('lodash')
const art = require('ascii-art')
const path = require('path')
const fs = require('fs')
const clipboardy = require('clipboardy')
const inquirer = require('inquirer');
const copy = require('copy-template-dir')
const assetsJsRoot = 'resources/assets/js'

async function getDistDir(options) {
  const {projectType, moduleName} = options
  let ctx = process.cwd()
  let isExistAssetsJsRoot = fs.existsSync(path.join(ctx, assetsJsRoot))
  let root = path.join(__dirname, `../output/${projectType}/${moduleName}`)
  if (isExistAssetsJsRoot) {
    root = path.join(ctx, assetsJsRoot, moduleName)
  }
  return root
}

async function buildTemplates(options) {
  const {projectType, moduleName} = options
  const templateDir = path.join(__dirname, `../templates/${projectType}/page`)
  const distDir = await getDistDir(options)
  console.log(distDir)
  let _s = moduleName.split('/')
  let realModuleName = _s[_s.length - 1]
  copy(
    templateDir,
    distDir,
    {
      name: realModuleName
    },
    (err, createdFiles) => {
      if (err)
        throw err
      createdFiles.forEach(filePath => console.log(chalk.green(`Created ${filePath}`))
      )
      console.log(chalk.blue('done!'))
      art.font('DONE', 'Doom', 'green', render => {
        console.log(render)
      })
    }
  )
}

async function generatorTemplate(options = {}, context = process.cwd()) {
  let opt = await inquirer
    .prompt([
      {
        type: 'list',
        name: 'projectType',
        message: '选择对应的项目?',
        choices: [
          'iogym',
          'zhijie_admin',
        ]
      },
      {
        type: 'input',
        name: 'moduleName',
        message: '模块名称?'
      }
    ])
  let result = await buildTemplates(opt)
}

module.exports = {
  build: (...args) => {
    return generatorTemplate(...args)
  },
  buildTemplates
} 
